package com.qinker.robot.hook.schedule;


import com.qinker.robot.hook.aio.ClientStart;
import com.qinker.robot.hook.aio.senter.RobotSenter;
import com.sky.xposed.common.util.Alog;

import org.smartboot.ioc.transport.NioSession;

import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 服务端通信心跳
 * Created by Irany 2019/10/30 14:23
 */
public class RobotHeartbeat {
    public static void heartbeat() {
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                try {
                    Alog.d("服务端通信心跳...");
                    NioSession session = ClientStart.getSession();
                    if (session == null || session.isInvalid()) {
                        Alog.i("机器人服务端失去连接,正在尝试重新连接...");
                        new RobotSenter().login();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        ScheduledExecutorService service = Executors.newScheduledThreadPool(1);
        service.scheduleWithFixedDelay(task, 5,5, TimeUnit.SECONDS);
    }
}
